package org.benjamin.tenancy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.tenancy.domain.BJUIResult;
import org.benjamin.tenancy.domain.BuildingForm;
import org.benjamin.tenancy.domain.entity.Building;
import org.benjamin.tenancy.domain.entity.Housing;
import org.benjamin.tenancy.domain.entity.Tenant;
import org.benjamin.tenancy.service.BuildingService;
import org.benjamin.tenancy.service.HousingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "housing")
public class HousingController {
	
	private final Logger logger = LogManager.getLogger();
	
	@Autowired
	private BuildingService buildingService;
	
	@Autowired
	private HousingService housingService;
	
	@RequestMapping(value = "index")
	public String index(Integer buildingId,ModelMap modelMap){
		UserDetails userDetails =  (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<Building> buildings = this.buildingService.findBuildingByUser(userDetails.getUsername(), false);
		modelMap.addAttribute("buildings",buildings);
		if(buildingId != null){
			List<Housing> housings = this.housingService.findByHousingBuildingId(buildingId);
			modelMap.addAttribute("housings",housings);
		}else{
			if(buildings.size() > 0){
				List<Housing> housings = this.housingService.findByHousingBuildingId(buildings.get(0).getId());
				modelMap.addAttribute("housings",housings);
			}
		}
		return "housing.index";
	}
	
	@GetMapping(value = "initial")
	public String initial() {
		return "housing.initial";
	}
	
	@RequestMapping(value = "toEdit")
	public String edit(ModelMap modelMap){
		modelMap.addAttribute("buildingForm",new BuildingForm());
		return "housing/edit";
	}
	
	@ResponseBody
	@RequestMapping(value = "edit")
	public BJUIResult doEdit(BuildingForm buildingForm){
		logger.info("请求接受的参数：{}",buildingForm);
		this.buildingService.analyseAndSave(buildingForm);
		return BJUIResult.defaultSuccess("成功");
	}
	
	@GetMapping(value = "details/{id}")
	public String toDetails(@PathVariable("id") String housingId){
		logger.info("查看");
		return "housing.details";
	}
	
	/**
	 * 入住页面
	 * @return
	 */
	@RequestMapping(value = "toCheckIn/{id}")
	public String checkIn(@PathVariable("id")Integer id,ModelMap modelMap){
		Housing housing = this.housingService.findEntityById(id);
		Tenant tenant = housing.getTenant();
		if(tenant == null){
			logger.info("租客信息是NULL");
			tenant = new Tenant();
			tenant.setId(id);
		}
		modelMap.addAttribute("tenant",tenant);
		return "housing.checkIn";
	}
	
	/**
	 * 客户入住
	 * @return
	 */
	@RequestMapping(value = "do_check_in/{housingId}")
	public String doCheckIn(@PathVariable("housingId") Integer housingId,Tenant tenant,HttpServletRequest request) {

		this.housingService.checkIn(housingId,tenant);
		return "result.success";
	}
}
